package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.MedicalDao;
import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.domain.medical.S201;
import com.gxuwz.medical.tools.StringUtil;

/**
 *医疗机构管理控制处理模块
 * @author 演示
 *
 */
public class MedicalServlet extends BaseServlet {
    MedicalDao medicalDao=new MedicalDao();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String  m=request.getParameter("m");//动作类型参数
		if("list".equals(m)){
			process(request, response, "/page/medical/medical_list.jsp");
		}else if("input".equals(m)){
			process(request, response, "/page/medical/medical_add.jsp");
		}else if("get".equals(m)){
			process(request, response, "/page/medical/medical_edit.jsp");
		}else if("add".equals(m)){
			add(request, response);
			process(request, response, "/page/medical/medical_list.jsp");
		}else if ("edit".equals(m)) {
			edit(request, response);
		}else if ("del".equals(m)) {
			del(request, response);
		}else{
			error(request, response);
		}  
	}
	/**
	 * 添加机构方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		 //接收页面传递过来的参数
		String jgbm=request.getParameter("jgbm");
		String zzjgbm=request.getParameter("zzjgbm");
		String jgmc=request.getParameter("jgmc");
		String dqbm=request.getParameter("dqbm");
		String areacode=request.getParameter("areacode");
		String strlsgx=request.getParameter("lsgx");
		String strjgjb=request.getParameter("jgjb");
		String strsbddlx=request.getParameter("sbddlx");
		String strpzddlx=request.getParameter("pzddlx");
		String strssjjlx=request.getParameter("ssjjlx");
		String strwsjgdl=request.getParameter("wsjgdl");
		String strwsjgxl=request.getParameter("wsjgxl");
		String zgdw=request.getParameter("zgdw");
		String kysj=request.getParameter("kysj");//日期
		String frdb=request.getParameter("frdb");
		String zczjnum=request.getParameter("zczj");//数值		
		double zczj=Double.parseDouble(zczjnum);

		S201 lsgx=new S201(strlsgx, "", "02");
		S201 jgjb=new S201(strjgjb, "", "06");
		S201 sbddlx=new S201(strsbddlx, "", "04");
		S201 pzddlx=new S201(strpzddlx, "", "04");
		S201 ssjjlx=new S201(strssjjlx, "", "01");
		S201 wsjgdl=new S201(strwsjgdl, "", "03");
		S201 wsjgxl=new S201(strwsjgxl, "", "0301");
		jgbm=StringUtil.trim(jgbm);
		 //调用添加方法
		 try{
			 Medical model=new Medical(jgbm, zzjgbm, jgmc, dqbm, areacode, lsgx, jgjb, sbddlx, pzddlx, ssjjlx, wsjgdl, wsjgxl, zgdw, kysj, frdb, zczj);
			 model.add();
		 }catch(Exception e){
			 
			 error(request, response);
		 }
		 
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
		Object[] params= new Object[11];
		params[0]=request.getParameter("zzjgbm");
		params[1]=request.getParameter("jgmc");
		params[2]=request.getParameter("dqbm");
		params[3]=request.getParameter("areacode");
		params[4]=request.getParameter("lsgx");
		params[5]=request.getParameter("jgjb");
		params[6]=request.getParameter("zgdw");
		params[7]=request.getParameter("kysj");
		params[8]=request.getParameter("frdb");
		params[9]=request.getParameter("zczj");
		params[10]=request.getParameter("jgbm");
		try{
			this.medicalDao.editOrdelObject("update t_medical set zzjgbm=?,jgmc=?,dqbm=?,areacode=?,lsgx=?,jgjb=?,zgdw=?,kysj=?,frdb=?,zczj=? where jgbm=?", params);
			process(request, response, "/page/medical/medical_list.jsp");
		}catch(Exception e){
			request.setAttribute("error", "医疗机构编码重复");
			process(request, response, "/error.jsp");
			e.printStackTrace();
		}	
	}
    private void del(HttpServletRequest request, HttpServletResponse response) {
    	Object[] params= new Object[1];
		params[0]=request.getParameter("id");
		//2:调用方法
		try{
			this.medicalDao.editOrdelObject("delete from t_medical where jgbm=?", params);
			process(request, response, "/page/medical/medical_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
}
